<?php

namespace zhtfrontend\controllers;

use common\components\helpers\Fun;
use zhtfrontend\models\datas\Arrangement;
use zhtfrontend\models\datas\ClassManage;
use zhtfrontend\models\datas\ClassManageAttribute;
use zhtfrontend\models\datas\ClassManageGoods;
use zhtfrontend\models\datas\ClassManageTeacher;
use zhtfrontend\models\datas\ClassManageTime;
use zhtfrontend\models\datas\ClassroomAttr;
use zhtfrontend\models\datas\Goods;
use zhtfrontend\models\datas\GoodsAttr;
use zhtfrontend\models\datas\GoodsClassTime;
use zhtfrontend\models\datas\GoodsPrice;
use zhtfrontend\models\datas\GoodsTeacher;
use zhtfrontend\models\datas\Teacher;

class BatchexecutiveController extends \zhtfrontend\components\bases\BaseController
{
    public function actionIndex()
    {
        $page = \Yii::$app->request->get('page');
        $offset = ($page-1)*20;
        $arrModel = Arrangement::find()->where(['>','id',1959])->offset($offset)->limit(20)->all();
        foreach ($arrModel as $item){
            $modelClassManage = new ClassManage();
            $modelClassManage->mechanic_id = isset($item->course->mechince_id) ? $item->course->mechince_id : 0;
            $modelClassManage->class_code = 'c' . time();
            $modelClassManage->code = $item->id;
            $modelClassManage->is_book = $item->is_booking;
            $modelClassManage->is_old = 0;
            if ($modelClassManage->save(false)){
                //生成班级属性
                $modelClassManageAttr = new ClassManageAttribute();
                $modelClassManageAttr->class_id = $modelClassManage->class_id;
                $modelClassManageAttr->course_id = $item->course_id;
                $modelClassManageAttr->course = isset($item->course->name) ? $item->course->name : '';
//                        $modelClassManageAttr->teacher_id = $model->teacher_id;
//                        $modelClassManageAttr->teacher = isset($model->teacher->name) ? $model->teacher->name : '';
                $modelClassManageAttr->room_id = $item->classroom_id;
                $modelClassManageAttr->classroom_name = isset($item->classroom->name)?$item->classroom->name:'';;
                $modelClassManageAttr->classroom_number = isset($item->classroom->room_number) ? $item->classroom->room_number : '';
                $modelClassManageAttr->teacher_form = isset($item->course->teache_form) ? $item->course->teache_form : 0;
                $modelClassManageAttr->price = $item->price;
                $modelClassManageAttr->class_count = $item->class_count;
                $modelClassManageAttr->suitable_age = $item->suitable_age;
                $modelClassManageAttr->age_group = $item->age_group;
                $modelClassManageAttr->class_length = $item->class_length;
                $modelClassManageAttr->max_number = $item->max_number;
                $modelClassManageAttr->opening_number = $item->opening_number;
                $modelClassManageAttr->is_booking = $item->is_booking;
                $modelClassManageAttr->region = '李沧区';
                $modelClassManageAttr->tutor_id = $item->tutor_id;
                $modelClassManageAttr->is_group = $item->is_group;
                $modelClassManageAttr->arrangement_id = $item->id;
                $modelClassManageAttr->room_group_id = 0;
                $modelClassManageAttr->room_group_name = '';
                $modelClassManageAttr->room_type = $item->room_type;
                $modelClassManageAttr->updated_at = $item->updated_at;
                $modelClassManageAttr->class_hour = $item->class_hour;
                $modelClassManageAttr->save(false);

                if (!empty($item->classroom_attr_id)) {
                    $timeArr = explode(',', $item->classroom_attr_id);
                    $classroom_attr = ClassroomAttr::find()->where(['id' => $timeArr])->all();
                    if ($classroom_attr != null) {
                        foreach ($classroom_attr as $k => $v) {
                            $modelClassManageTime = new ClassManageTime();
                            $modelClassManageTime->class_id = $modelClassManage->class_id;
                            $modelClassManageTime->class_day = $v->classday;
                            $len = strlen($v->start_time);
                            $length = strlen($v->end_time);
                            if ($len == 1) {
                                $start_hour = '00';
                                $start_minute = '0' . $v->start_time;
                            } elseif ($len == 2) {
                                $start_hour = '00';
                                $start_minute = $v->start_time;
                            } elseif ($len == 3) {
                                $start_hour = '0' . floor($v->start_time / 100);
                                $start_minute = substr($v->start_time, 1);
                            } else {
                                $start_hour = floor($v->start_time / 100);
                                $start_minute = substr($v->start_time, 2);
                            }
                            if ($length == 1) {
                                $end_hour = '00';
                                $end_minute = '0' . $v->end_time;
                            } elseif ($length == 2) {
                                $end_hour = '00';
                                $end_minute = $v->start_time;
                            } elseif ($length == 3) {
                                $end_hour = '0' . floor($v->end_time / 100);
                                $end_minute = substr($v->end_time, 1);
                            } else {
                                $end_hour = floor($v->end_time / 100);
                                $end_minute = substr($v->end_time, 2);
                            }
                            $start_time = $start_hour . ':' . $start_minute;
                            $end_time = $end_hour . ':' . $end_minute;
                            $modelClassManageTime->start_time = $start_time;
                            $modelClassManageTime->end_time = $end_time;
                            $modelClassManageTime->classroom_attr_id = $v->id;
                            $modelClassManageTime->save(false);
                        }
                    }
                }

                if(!empty($item->teacher_id)){
                    $teacherArr = explode(',', $item->teacher_id);
                    foreach ($teacherArr as $id){
                        $modelTeacher = Teacher::findOne($id);
                        $modelClassteacher = new ClassManageTeacher();
                        $modelClassteacher->class_id = $modelClassManage->class_id;
                        $modelClassteacher->teacher_id = $id;
                        $modelClassteacher->teacher_name = $modelTeacher->name;
                        $modelClassteacher->save(false);
                    }
                }
            }
        }
    }

    public function actionBatchgoods(){
        $page = \Yii::$app->request->get('page');
        $offset = ($page-1)*20;
        $clasList = ClassManage::find()->where(['>','class_id',1639])->offset($offset)->limit(20)->all();
        $arr = ['region', 'is_booking', 'class_count', 'max_number', 'opening_number', 'course', 'classroom_number', 'classroom_name', 'suitable_age', 'class_length', 'teacher_form', 'age_group'];
        foreach ($clasList as $model){
            $modelGoods = new Goods();
            $classAttr = ClassManageAttribute::find()->where(['class_id' => $model->class_id])->one();
            $classTime = ClassManageTime::find()->where(['class_id' => $model->class_id])->all();
            $classTeacher = ClassManageTeacher::find()->where(['class_id' => $model->class_id])->all();
            $modelGoods->name = $classAttr->course;
            $modelGoods->sn = 'course' . time();
            $modelGoods->cat_id = isset($classAttr->courses->category_id) ? $classAttr->courses->category_id : 0;
            $modelGoods->type_id = 1;
            $modelGoods->price = $classAttr->price;
            $modelGoods->market_price = $classAttr->price;
            $modelGoods->store = $classAttr->max_number;
            $modelGoods->picture = '';
            $modelGoods->description = isset($classAttr->teachers->description) ? $classAttr->teachers->description : '';
            $modelGoods->content = isset($classAttr->courses->description) ? $classAttr->courses->description : '';
            $modelGoods->is_shelve = 0;
            $modelGoods->is_delete = 0;
            $modelGoods->is_booking = $classAttr->is_booking;
            $modelGoods->is_buy = $classAttr->is_buy;
            $modelGoods->site_id = 2;
            $modelGoods->mechanic_id = $model->mechanic_id;
            $modelGoods->max_buy = 1;
            $modelGoods->up_at = time();
            $modelGoods->updated_at = $classAttr->updated_at;
            $modelGoods->admin_id = \Yii::$app->user->id;
            $modelGoods->arrangement_id = $classAttr->arrangement_id;
            $modelGoods->is_group = $classAttr->is_group;
            $modelGoods->category_id = isset($classAttr->courses->type_id) ? $classAttr->courses->type_id : 0;
            $modelGoods->teaching_methods = 1;
            $modelGoods->deposit = 200;
            $modelGoods->tutor_id = $classAttr->tutor_id;
            $modelGoods->class_hour = $classAttr->class_hour;
            $modelGoods->status = 1;
            $modelGoods->save(false);
            //设置价格
            $insertArr = [
                ['price_name'=>'三个月','lesson'=>12],
                ['price_name'=>'半年','lesson'=>24],
                ['price_name'=>'一年','lesson'=>48]
            ];
            foreach ($insertArr as $val){
                $goodsPrice = new GoodsPrice();
                $goodsPrice->goods_id = $modelGoods->id;
                $goodsPrice->price_name = $val['price_name'];
                $goodsPrice->lesson = 12;
                $goodsPrice->lesson_price = $val['lesson']*$modelGoods->price;
                $goodsPrice->distribution_way = 1;
                $goodsPrice->distribution_proportion = 30;
                $goodsPrice->is_scholarship = 2;
                $goodsPrice->admin_id = 1;
                $goodsPrice->art_lesson = 6;
                $goodsPrice->art_lesson_price = 300;
                $goodsPrice->is_art_scholarship = 2;
                $goodsPrice->market_price = $val['lesson']*$modelGoods->price;
                $goodsPrice->save(false);
            }

//            if($modelGoods->is_booking == 1){
//                $goodsPrice->price_name = '奖学金课程';
//                $goodsPrice->lesson = 6;
//                $goodsPrice->lesson_price = 6*$modelGoods->price;
//                $goodsPrice->subsidy_type = 1;
//                $goodsPrice->subsidy_money = 200;
//                $goodsPrice->is_scholarship = 1;
//                $goodsPrice->scholarship = 6*$modelGoods->price;
//                $goodsPrice->art_lesson = 3;
//                $goodsPrice->art_lesson_price = 150;
//                $goodsPrice->is_art_scholarship = 1;
//                $goodsPrice->art_scholarship = 150;
//                $goodsPrice->market_price = 6*$modelGoods->price+150;
//            }

            foreach ($classAttr as $key => $val) {
                if (in_array($key, $arr)) {
                    if ($key == 'region') {
                        $attributes = new GoodsAttr(['goods_id' => $modelGoods->id, 'attribute_id' => $classAttr->arrangement_id, 'name' => '地域', 'value' => $classAttr->region, 'obj_type' => 'region']);
                        $attributes->save(false);
                    }
                    if ($key == 'is_booking') {
                        $attributes = new GoodsAttr(['goods_id' => $modelGoods->id, 'attribute_id' => $classAttr->arrangement_id, 'name' => '是否可预约', 'value' => $classAttr->is_booking, 'obj_type' => 'is_booking']);
                        $attributes->save(false);
                    }
                    if ($key == 'class_count') {
                        $attributes = new GoodsAttr(['goods_id' => $modelGoods->id, 'attribute_id' => $classAttr->arrangement_id, 'name' => '上课次数', 'value' => $classAttr->class_count, 'obj_type' => 'class_count']);
                        $attributes->save(false);
                    }
                    if ($key == 'max_number') {
                        $attributes = new GoodsAttr(['goods_id' => $modelGoods->id, 'attribute_id' => $classAttr->arrangement_id, 'name' => '最满人数', 'value' => $classAttr->max_number, 'obj_type' => 'max_number']);
                        $attributes->save(false);
                    }
                    if ($key == 'opening_number') {
                        $attributes = new GoodsAttr(['goods_id' => $modelGoods->id, 'attribute_id' => $classAttr->arrangement_id, 'name' => '开班人数', 'value' => $classAttr->opening_number, 'obj_type' => 'opening_number']);
                        $attributes->save(false);
                    }
                    if ($key == 'course') {
                        $attributes = new GoodsAttr(['goods_id' => $modelGoods->id, 'attribute_id' => $classAttr->course_id, 'name' => '课程', 'value' => $classAttr->course, 'obj_type' => 'course']);
                        $attributes->save(false);
                    }
                    if ($key == 'classroom_number') {
                        $attributes = new GoodsAttr(['goods_id' => $modelGoods->id, 'attribute_id' => $classAttr->room_id, 'name' => '教室编号', 'value' => $classAttr->classroom_number, 'obj_type' => 'classroom_number']);
                        $attributes->save(false);
                    }
                    if ($key == 'classroom_name') {
                        if ($classAttr->room_type == 1) {
                            $room_id = $classAttr->room_group_id;
                            $classroom_name = '1-' . $classAttr->room_group_name;
                        } else {
                            $room_id = $classAttr->room_id;
                            $classroom_name = '0-' . $classAttr->classroom_name;
                        }
                        $attributes = new GoodsAttr(['goods_id' => $modelGoods->id, 'attribute_id' => $room_id, 'name' => '教室名称', 'value' => $classroom_name, 'obj_type' => 'classroom_name']);
                        $attributes->save(false);
                    }
                    if ($key == 'suitable_age') {
                        $attributes = new GoodsAttr(['goods_id' => $modelGoods->id, 'attribute_id' => $classAttr->arrangement_id, 'name' => '适合年龄段', 'value' => $classAttr->suitable_age, 'obj_type' => 'suitable_age']);
                        $attributes->save(false);
                    }
                    if ($key == 'age_group') {
                        $attributes = new GoodsAttr(['goods_id' => $modelGoods->id, 'attribute_id' => $classAttr->arrangement_id, 'name' => '年龄段', 'value' => $classAttr->age_group, 'obj_type' => 'age_group']);
                        $attributes->save(false);
                    }
                    if ($key == 'class_length') {
                        $attributes = new GoodsAttr(['goods_id' => $modelGoods->id, 'attribute_id' => $classAttr->arrangement_id, 'name' => '上课时长', 'value' => $classAttr->class_length, 'obj_type' => 'class_length']);
                        $attributes->save(false);
                    }
                    if ($key == 'teacher_form') {
                        $attributes = new GoodsAttr(['goods_id' => $modelGoods->id, 'attribute_id' => $classAttr->arrangement_id, 'name' => '授课方式', 'value' => $classAttr->teacher_form, 'obj_type' => 'teacher_form']);
                        $attributes->save(false);
                    }
                }
            }

            foreach ($classTime as $time) {
                $GoodsTime = new GoodsClassTime();
                $GoodsTime->goods_id = $modelGoods->id;
                $GoodsTime->class_day = $time->class_day;
                $GoodsTime->start_time = $time->start_time;
                $GoodsTime->end_time = $time->end_time;
                $GoodsTime->classroom_attr_id = $time->classroom_attr_id;
                $GoodsTime->save(false);
            }
            foreach ($classTeacher as $teacher) {
                $GoodsTeacher = new GoodsTeacher();
                $GoodsTeacher->goods_id = $modelGoods->id;
                $GoodsTeacher->teacher_id = $teacher->teacher_id;
                $GoodsTeacher->teacher_name = $teacher->teacher_name;
                $GoodsTeacher->save(false);
            }
            $ClassManageGoods = new ClassManageGoods();
            $ClassManageGoods->class_id = $model->class_id;
            $ClassManageGoods->goods_id = $modelGoods->id;
            $ClassManageGoods->created_at = $classAttr->updated_at;
            $ClassManageGoods->save(false);
        }
    }


}
